import { useAppDispatch, useAppSelector } from "@/store/hooks";
import { submitWhiteRegister } from "@/store/whiteRegister/whiteRegisterAPI";
import { getWhiteRegisterAsync } from "@/store/whiteRegister/whiteRegisterSlice";
import { Button, List, Modal } from "antd";
import type { ReactElement } from "react";

export default function Table(): ReactElement<typeof List, "div"> {
  const { table, params } = useAppSelector((state) => state.whiteRegister);
  const dispatch = useAppDispatch()
  const handleClick = (android_id:string) => {
    Modal.confirm({
      title: '确认删除吗？',
      onOk: () => new Promise<void>((resolve, reject) => {
        submitWhiteRegister({
          android_id,
          app_params: "zxcvbn1",
          params_type: 2
        }).then(() => {
          resolve()
          dispatch(getWhiteRegisterAsync({...params}))
        }).catch(reject)
      })
    })
  }
  return (
    <List
      className="bg-white"
      loading={table.status === "loading"}
      dataSource={table.data?.data}
      bordered
      size="small"
      renderItem={(item) => (
        <List.Item
          actions={[<Button type="link" danger onClick={() => handleClick(item)}>删除</Button>]}
        >
          {item}
        </List.Item>
      )}
    />
  );
}
